//清除缓存
localStorage.clear();
 
$(".btn-login").on('click', function() {
	//防止重复点击
	$(this).attr("disabled", "disabled");
	var username = $('.user-input').val();
	var password = $('.pwd-input').val();
	if(!username) {
		layer.msg("用户名不能为空");
		$(this).removeAttr("disabled");
		return false;
	}
	if(!password) {
		layer.msg("登录密码不能为空");
		$(this).removeAttr("disabled");
		return false;
	}
	var _that=$(this);
	var d = {};
	d.username = username;
	d.password = password;
	console.log(url);
	$.ajax({
		url: url + "/sys/login",
		type: 'POST',
		//		参数需转为字符串
		data: JSON.stringify(d),
		//		json跨域
		dataType: 'json',
		//		contentType类型为json
		contentType: "application/json",

		//		请求头加上X-Token 
		 beforeSend: function(XMLHttpRequest) {
			//		token 为页面所获取
			   
//			XMLHttpRequest.setRequestHeader("X-Token", token); 
		},
		useDefaultXhrHeader: false,
		success: function(result) {
			if(result.code == '0') {
				layer.msg("登录成功");
				var data=result.data;
				if(data.userType == '0'){
					localStorage.setItem("username_type1",data.username);
					localStorage.setItem("name_type1",data.name);
					location.href="dspIndex.html?token="+data.token+"&id="+data.id;
				}else if(data.userType == '1'){
					location.href="../daili2/ads.ahdsp.com/account.html?token="+data.token+"&id="+data.id;
				}else if(data.userType == '2'){
					sessionStorage.setItem("token",data.token);
                                        sessionStorage.setItem("userId",data.id);
					location.href="../guanggaozhu/ads.ahdsp.com/agentIndex.html?token="+data.token+"&id="+data.id;
				}
				
			} else {
				isErr(result);
			}
		},
		err: function(err) {
			layer.msg("请求失败");
		},
		complete: function() {
			$(_that).removeAttr("disabled");
		}
	});
});
window.onload = function(){
	var username= GetQueryString("username");
//      username = encodeURI(username);
	var loginPwd = GetQueryString("password");
	var datas1 = {username:username,password:loginPwd}; 
	if(username && loginPwd){
		$.ajax({
		url: url + "/sys/login",
		type: 'POST',
		data: JSON.stringify(datas1),
		dataType: 'json',
		contentType: "application/json",
		useDefaultXhrHeader: false,
		success: function(result) {
			if(result.code == '0') {
				layer.msg("登录成功");
				var data=result.data;
				if(data.userType == '0'){
					location.href="dspIndex.html?token="+data.token+"&id="+data.id;
				}else if(data.userType == '1'){
					location.href="../daili2/ads.ahdsp.com/account.html?token="+data.token+"&id="+data.id;
				}else if(data.userType == '2'){
					sessionStorage.setItem("token",data.token);
                                        sessionStorage.setItem("userId",data.id);
					location.href="../guanggaozhu/ads.ahdsp.com/agentIndex.html?token="+data.token+"&id="+data.id;
				}
				
			} else {
				isErr(result);
			}
		},
		err: function(err) {
			layer.msg("请求失败");
		}
		
	});
	}
	
}
